package mint.mvc.core.upload;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URI;
import java.nio.channels.FileChannel;

/** 
 * 文件类，添加了一个saveTo方法
 * @author LiangWei(895925636@qq.com)
 * @date 2015年8月9日 下午15:30:43 
 */
public class FileUploaded extends File{
	private static final long serialVersionUID = 1L;

	public FileUploaded(URI uri) {
		super(uri);
	}

	public FileUploaded(String parent, String child) {
		super(parent, child);
	}

	public FileUploaded(String pathname) {
		super(pathname);
	}

	public FileUploaded(File parent, String child) {
		super(parent, child);
	}

	/**
	 * 将被文件复制到指定的目标位置
	 * @param path 路径和文件名字符串
	 * @throws IOException
	 */
	public void saveAs(String path) throws IOException{
		FileChannel in = null;
		FileChannel out = null;
		try {
			in = new FileInputStream(this).getChannel();			//得到对应的文件通道
			out = new FileOutputStream(new File(path)).getChannel();	//得到对应的文件通道
			in.transferTo(0, in.size(), out);						//连接两个通道，并且从in通道读取，然后写入out通道
		} catch (IOException e) {
			throw e;
		} finally {
			try {
				if(in != null) in.close();
				if(out != null) out.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	
	/**
	 * 将被文件复制到指定的目标位置
	 * @param path 路径, end with "/"
	 * @param name 文件名字符串
	 * @throws IOException
	 */
	public void saveAs(String path, String name) throws IOException{
		saveAs(path+name);
	}
}
